home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / BitSet.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  3.7 KB  |  123 lines

  1. //: C20:BitSet.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Exercising the bitset class
  7. #include <iostream>
  8. #include <bitset>
  9. #include <cstdlib>
  10. #include <ctime>
  11. #include <climits>
  12. #include <string>
  13. using namespace std;
  14. const int sz = 32;
  15. typedef bitset<sz> BS;
  16.  
  17. template<int bits>
  18. bitset<bits> randBitset() {
  19.   bitset<bits> r(rand());
  20.   for(int i = 0; i < bits/16 - 1; i++) {
  21.     r <<= 16;
  22.     // "OR" together with a new lower 16 bits:
  23.     r |= bitset<bits>(rand()); 
  24.   }
  25.   return r;
  26. }  
  27.  
  28. int main() {
  29.   srand(time(0));
  30.   cout << "sizeof(bitset<16>) = " 
  31.     << sizeof(bitset<16>) << endl;
  32.   cout << "sizeof(bitset<32>) = " 
  33.     << sizeof(bitset<32>) << endl;
  34.   cout << "sizeof(bitset<48>) = " 
  35.     << sizeof(bitset<48>) << endl;
  36.   cout << "sizeof(bitset<64>) = " 
  37.     << sizeof(bitset<64>) << endl;
  38.   cout << "sizeof(bitset<65>) = " 
  39.     << sizeof(bitset<65>) << endl;
  40.   BS a(randBitset<sz>()), b(randBitset<sz>());
  41.   // Converting from a bitset:
  42.   unsigned long ul = a.to_ulong();
  43.   string s = b.to_string();
  44.   // Converting a string to a bitset:
  45.   char* cbits = "111011010110111";
  46.   cout << "char* cbits = " << cbits <<endl;
  47.   cout << BS(cbits) << " [BS(cbits)]" << endl;
  48.   cout << BS(cbits, 2) 
  49.     << " [BS(cbits, 2)]" << endl;
  50.   cout << BS(cbits, 2, 11)
  51.     << " [BS(cbits, 2, 11)]" << endl;
  52.   cout << a << " [a]" << endl;
  53.   cout << b << " [b]"<< endl;
  54.   // Bitwise AND:
  55.   cout << (a & b) << " [a & b]" << endl;
  56.   cout << (BS(a) &= b) << " [a &= b]" << endl;
  57.   // Bitwise OR:
  58.   cout << (a | b) << " [a | b]" << endl;
  59.   cout << (BS(a) |= b) << " [a |= b]" << endl;
  60.   // Exclusive OR:
  61.   cout << (a ^ b) << " [a ^ b]" << endl;
  62.   cout << (BS(a) ^= b) << " [a ^= b]" << endl;
  63.   cout << a << " [a]" << endl; // For reference
  64.   // Logical left shift (fill with zeros):
  65.   cout << (BS(a) <<= sz/2) 
  66.     << " [a <<= (sz/2)]" << endl;
  67.   cout << (a << sz/2) << endl;
  68.   cout << a << " [a]" << endl; // For reference
  69.   // Logical right shift (fill with zeros):
  70.   cout << (BS(a) >>= sz/2) 
  71.     << " [a >>= (sz/2)]" << endl;
  72.   cout << (a >> sz/2) << endl;
  73.   cout << a << " [a]" << endl; // For reference
  74.   cout << BS(a).set() << " [a.set()]" << endl;
  75.   for(int i = 0; i < sz; i++)
  76.     if(!a.test(i)) {
  77.       cout << BS(a).set(i) 
  78.         << " [a.set(" << i <<")]" << endl;
  79.       break; // Just do one example of this
  80.     }
  81.   cout << BS(a).reset() << " [a.reset()]"<< endl;
  82.   for(int j = 0; j < sz; j++)
  83.     if(a.test(j)) {
  84.       cout << BS(a).reset(j) 
  85.         << " [a.reset(" << j <<")]" << endl;
  86.       break; // Just do one example of this
  87.     }
  88.   cout << BS(a).flip() << " [a.flip()]" << endl;
  89.   cout << ~a << " [~a]" << endl;  
  90.   cout << a << " [a]" << endl; // For reference
  91.   cout << BS(a).flip(1) << " [a.flip(1)]"<< endl;
  92.   BS c;
  93.   cout << c << " [c]" << endl;
  94.   cout << "c.count() = " << c.count() << endl;
  95.   cout << "c.any() = " 
  96.     << (c.any() ? "true" : "false") << endl;
  97.   cout << "c.none() = " 
  98.     << (c.none() ? "true" : "false") << endl;
  99.   c[1].flip(); c[2].flip();
  100.   cout << c << " [c]" << endl;
  101.   cout << "c.count() = " << c.count() << endl;
  102.   cout << "c.any() = " 
  103.     << (c.any() ? "true" : "false") << endl;
  104.   cout << "c.none() = " 
  105.     << (c.none() ? "true" : "false") << endl;
  106.   // Array indexing operations:
  107.   c.reset();
  108.   for(int k = 0; k < c.size(); k++)
  109.     if(k % 2 == 0)
  110.       c[k].flip();
  111.   cout << c << " [c]" << endl;
  112.   c.reset();
  113.   // Assignment to bool:
  114.   for(int ii = 0; ii < c.size(); ii++)
  115.     c[ii] = (rand() % 100) < 25;
  116.   cout << c << " [c]" << endl;
  117.   // bool test:
  118.   if(c[1] == true) 
  119.     cout << "c[1] == true"; 
  120.   else 
  121.     cout << "c[1] == false" << endl;
  122. } ///:~
  123.